package com.gxy.learn.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.gxy.learn.mybatisplus.common.Result;
import com.gxy.learn.mybatisplus.entity.SysUser;
import com.gxy.learn.mybatisplus.mapper.SysUserMapper;
import com.gxy.learn.mybatisplus.service.SysUserService;
import com.gxy.learn.mybatisplus.vo.ListPageReqVO;
import com.gxy.learn.mybatisplus.vo.SysUserInfoVO;
import com.gxy.learn.mybatisplus.vo.SysUserSaveVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.*;

/**
 * @author: Gao xueyong
 * Create at: 2022/1/4 下午11:53
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Override
    public Result save(SysUserSaveVO saveVO) {
        if (StringUtils.isBlank(saveVO.getId())) {
            SysUser sysUser = new SysUser();
            BeanUtils.copyProperties(saveVO, sysUser);
            baseMapper.insert(sysUser);
            return Result.success("操作成功！");
        }
        SysUser byId = getById(saveVO.getId());
        if (null == byId) {
            return Result.error("数据不存在！");
        }
        BeanUtils.copyProperties(saveVO, byId);
        baseMapper.updateById(byId);
        return Result.success("操作成功！");
    }

    @Override
    public IPage<SysUserInfoVO> listPage(ListPageReqVO reqVO) {
        return baseMapper.listPage(new Page<>(reqVO.getCurrent(), reqVO.getSize()), reqVO);
    }

    @Override
    public List<SysUserInfoVO> users(String companyId) {
        if (StringUtils.isBlank(companyId)) {
            return null;
        }
       return baseMapper.listUser(companyId);
    }

    @Override
    public SysUserInfoVO info(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        SysUser sysUser = baseMapper.selectById(id);
        if (null == sysUser) {
            return null;
        }
        SysUserInfoVO resVO = new SysUserInfoVO();
        BeanUtils.copyProperties(sysUser, resVO);
        return resVO;
    }

    @Override
    public void listTest() {
        ThreadFactory consumptionThreadFactory = new ThreadFactoryBuilder().setNameFormat("consumption-pool-%d").build();
        ExecutorService consumptionExecutor = new ThreadPoolExecutor(5, 10,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(10), consumptionThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(9);
        for(int i=0;i<100;i++){
            try {
                blockingQueue.put(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            consumptionExecutor.execute(()-> {
                long start = System.currentTimeMillis();
                List<SysUser> sysUsers = baseMapper.selectList(new LambdaQueryWrapper<>());
                log.info("线程- sysUsers.size() = {}",sysUsers.size());
                try {
                    log.info("当前任务[{}]", blockingQueue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                log.info("执行一次查询耗时：{}",System.currentTimeMillis()-start);
            });
            log.info("blockingQueue.size() = {}", blockingQueue.size());
        }
        consumptionExecutor.shutdown();

//        ExecutorService executorService = Executors.newFixedThreadPool(1);
//        executorService.execute(()->{
//                        List<SysUser> sysUsers = baseMapper.selectList(new LambdaQueryWrapper<>());
////            sysUsers.stream().forEach(x->{
////                x.setUpdateDate(new Date());
////                baseMapper.updateById(x);
////            });
//            log.info("线程- sysUsers = {}", JSONObject.toJSON(sysUsers));
//        });
    }


    @Override
    public void listTest2() {
        BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(5);
        int i = 0;
        while (i < 200) {
            i++;
            int finalI = i;
            try {
                blockingQueue.put(new Runnable() {
                    @Override
                    public void run() {
                        log.info("--- {}", finalI);
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
                log.info("blockingQueue.size() = {}", blockingQueue.size());

                if(blockingQueue.size() >=4){
                    Thread.sleep(2000);
                    for (int j = 0; j <4; j++) {
                        blockingQueue.take();
                    }
                    log.info("队列数据拿出完了");
                }
            } catch (InterruptedException e) {
                log.error("err ", e);
            }
        }
    }


}
